"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _vue = _interopRequireDefault(require("vue"));

var _ImagePreview = _interopRequireDefault(require("./ImagePreview"));

var _utils = require("../utils");

var instance;
var defaultConfig = {
    loop: true,
    images: [],
    value: true,
    minZoom: 1 / 3,
    maxZoom: 3,
    className: '',
    onClose: null,
    onChange: null,
    lazyLoad: false,
    showIndex: true,
    asyncClose: false,
    startPosition: 0,
    swipeDuration: 500,
    showIndicators: false,
    closeOnPopstate: false
};

var initInstance = function initInstance() {
    instance = new (_vue.default.extend(_ImagePreview.default))({
        el: document.createElement('div')
    });
    document.body.appendChild(instance.$el);
    instance.$on('change', function (index) {
        if (instance.onChange) {
            instance.onChange(index);
        }
    });
};

var ImagePreview = function ImagePreview(images, startPosition) {
    if (startPosition === void 0) {
        startPosition = 0;
    }

    /* istanbul ignore if */
    if (_utils.isServer) {
        return;
    }

    if (!instance) {
        initInstance();
    }

    var options = Array.isArray(images) ? {
        images: images,
        startPosition: startPosition
    } : images;
    (0, _extends2.default)(instance, defaultConfig, options);
    instance.$once('input', function (show) {
        instance.value = show;
    });

    if (options.onClose) {
        instance.$off('close');
        instance.$once('close', options.onClose);
    }

    return instance;
};

ImagePreview.install = function () {
    _vue.default.use(_ImagePreview.default);
};

var _default = ImagePreview;
exports.default = _default;